﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Signage01.dao
{    
    class TimelineDAO
    {
        string serializationFile = "resources/Timeline.bin";        
        static TimelineDAO timelineDAO = null;

        private TimelineDAO()
        {

        }

        public static TimelineDAO getTimelineSingleTon()
        {
            if (timelineDAO == null)
            {
                timelineDAO = new TimelineDAO();
            }
            return timelineDAO;
        }

        public  List<Timeline>  getTimelineObject()
        {
            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    List<Timeline> listTemp = (List<Timeline>)bformatter.Deserialize(stream);
                    return listTemp;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                List<Timeline> listTemp = new List<Timeline>();
                return listTemp;
            }
        }

        private List<String> getTimeLineNames()
        {
            List<String> listTimelineName = new List<String>();
            try
            {
                List<Timeline> listTimeline = getTimelineObject();

                foreach (Timeline tempTimeline in listTimeline)
                {
                    listTimelineName.Add(tempTimeline.Name);
                }
                return listTimelineName;
                
            }
            catch (Exception ex)
            {
                List<String> tempTimelineListName = new List<String>();
                return tempTimelineListName;
                
            }
        }


        public bool timelineNameExists(String name)
        {
            List<String> listTimelineName = getTimeLineNames();

            foreach (String tempName in listTimelineName)
            {
                if (name == tempName)
                {
                    return true;
                }
            }
            return false;
        }



        public int saveTimelineObject(Timeline newTimelineObj)
        {
            List<Timeline> listTimeline = getTimelineObject();
            listTimeline.Add(newTimelineObj);

            return saveListToObject(listTimeline);
        }

        public int deleteTimelineObject(Timeline timeline)
        {
            List<Timeline> listTimeline = getTimelineObject();

            int index = 0;
            int deleteIndex = -1;

            foreach (Timeline tempTimeline in listTimeline)
            {
                if (timeline.Name == tempTimeline.Name)
                {
                    deleteIndex = index;
                    break;
                }
                index++;
            }

            listTimeline.RemoveAt(deleteIndex);

            return saveListToObject(listTimeline);
        }

        private int saveListToObject(List<Timeline> listTimeline)
        {
            try
            {
                // saving the list to the file
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bformatter.Serialize(stream, listTimeline);
                    stream.Close();
                }
                return 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine("save excepiton is:" + ex.ToString());
                return 0;
            }
        }


    }



    

}
